<?php

declare(strict_types=1);
/**
 * 开放订单、商品分类、商品品牌、商品国家相关接口
 */
namespace App\Controller\Api;

use App\Model\PssLogModel\InterfaceRequest;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Service\Code\Normal;
use function EasySwoole\Spl\explode;
use Service\AppService\IdService;
class IndexController extends AbstractController
{
    /**
     * 开放订单、商品分类、商品品牌、商品国家相关接口入口，需要APPID接入
     * @param RequestInterface $request
     */
    public function serviceIndex( RequestInterface $request ) 
    {
        $parameters = $request->getAttribute('data')['param'];
        $requestMethod = $parameters['requestMethod'];
        $class = "App\\Service\\OrderService\\OrderInterfaceService\\".$requestMethod;
        if( !class_exists($class) ) {
            return Normal::returnCode(Normal::FAIL,"业务模块：{$requestMethod}不存在");
        }
        
        $action = 'index';
        //默认方法
        if( !method_exists($class,$action) ) {
            return Normal::returnCode(Normal::FAIL,'业务实体不存在');
        }
        
        $res = $class::instance()->{$action}($parameters);        
        $request_start_time = $request->getAttribute('request_start_time');
        $request_used_time  = bcsub((string)microtime(true),(string)$request_start_time,3);
        
        //记录数据
        InterfaceRequest::instance()->addLog([
            'interface_code'    => $requestMethod,
            'bid'               => $parameters['binfo']['bid'],
            'type'              => 1,
            'request_use_time'  => $request_used_time,
            'request_param'     => json_encode($parameters,JSON_UNESCAPED_UNICODE),
            'response_data'     => json_encode($res,JSON_UNESCAPED_UNICODE),
            'response_code'     => $res['code'],
            'response_msg'      => $res['msg'],
        ]);
        
        //收银台支付特殊处理
        if( $requestMethod == 'OrderWebCashPay' ) {
            if( $res['code'] == Normal::SUC ) {
                return $res['data'];
            } else {
                return $res['msg']??'处理失败';
            }
        }
        return $res;
    }

    /**
     * 外部取消订单订单接口不需要签名APPID接入（关务使用）
     * @param RequestInterface $request
     */
    public function serviceUnSignatureIndex( RequestInterface $request )
    {
        $parameters    = $request->post();
        if( empty($parameters['requestMethod']) ) {
            return Normal::returnCode(Normal::FAIL,'except param requestMethod');
        }
        
        if( empty($parameters['requestData']) ) {
            return Normal::returnCode(Normal::FAIL,"except param requestData");
        }
        
        $explodes = \explode(".",$parameters['requestMethod']);
        $requestMethod['module'] = $explodes[0]??'';
        $requestMethod['action'] = $explodes[1]??'';
        if( empty($requestMethod['module']) ) {
            return Normal::returnCode(Normal::FAIL,'except param requestMethod.module');
        }
        
        if( empty($requestMethod['action']) ) {
            return Normal::returnCode(Normal::FAIL,'except param requestMethod.action');
        }
        
        $routeInterface = $requestMethod['module'];
        $class = "App\\Service\\OrderService\\OrderInterfaceService\\".$routeInterface;
        if( !class_exists($class) ) {
            return Normal::returnCode(Normal::FAIL,"业务模块：{$routeInterface}不存在");
        }
        
        $action = $requestMethod['action'];
        if( !method_exists($class,$action) ) {
            return Normal::returnCode(Normal::FAIL,'业务实体不存在');
        }
        
        $res = $class::instance()->{$action}($parameters);
    
        //记录数据
        InterfaceRequest::instance()->addLog([
            'interface_code'    => $action,
            'bid'               => 0,
            'type'              => 1,
            'request_param'     => json_encode($parameters),
            'response_data'     => json_encode($res),
            'response_code'     => $res['code'],
            'response_msg'      => $res['msg'],
        ]);
        return $res;
    }
    
    /**
     * 获取雪花算法ID
     * @return string
     */
    public function getSnakeId()
    {
        return (string)IdService::instance()->getLogID();
    }
    
    /**
     * 服务健康检查
     */
    public function health() 
    {
        return "success";
    }
}
